<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Consignment extends CI_Controller {

    function __construct() {
        parent::__construct();
    }

    public function index($id = null) {
        $html = $this->init("代銷", "jquery,jqueryui,consignment", "");
        $this->load->model('consignment_management_model');
        $html['consignment_management'] = $this->consignment_management_model->get_info($id);
        $this->load->view('consignment', $html);
    }

    public function add() {
        $this->load->model('demand_model');
        $data['name'] = $this->input->post('name');
        $data['ms'] = $this->input->post('ms');
        $data['mr'] = $this->input->post('mr');
        $data['mobile'] = $this->input->post('mobile');
        $data['home_phone'] = $this->input->post('home_phone');
        $data['company_phone'] = $this->input->post('company_phone');
        $data['home_address'] = $this->input->post('home_address');
        $data['building_name'] = $this->input->post('building_name');
        $data['company_address'] = $this->input->post('company_address');
        $data['company_name'] = $this->input->post('company_name');

        $data['email'] = $this->input->post('email');

        $data['contact'] = $this->input->post('contact');
        $data['contact_phone'] = $this->input->post('contact_phone');
        if ($this->input->post('demand_area') != "")
            $data['demand_area'] = "/" . join("/", $this->input->post('demand_area')) . "/";
        $data['parking_number'] = $this->input->post('parking_number');
        $data['acceptable_mechanical'] = $this->input->post('acceptable_mechanical');
        $data['indoor_area'] = $this->input->post('indoor_area');
        $data['other_demand'] = $this->input->post('other_demand');
        $data['floors'] = $this->input->post('floors');
        $data['windows1'] = $this->input->post('windows1');
        $data['building_facing1'] = $this->input->post('building_facing1');
        $data['facing_the_household1'] = $this->input->post('facing_the_household1');
        $data['seat1'] = $this->input->post('seat1');
        $data['towards1'] = $this->input->post('towards1');
        $data['windows2'] = $this->input->post('windows2');
        $data['building_facing2'] = $this->input->post('building_facing2');
        $data['facing_the_household2'] = $this->input->post('facing_the_household2');
        $data['seat2'] = $this->input->post('seat2');
        $data['towards2'] = $this->input->post('towards2');
        $data['households_demand'] = $this->input->post('households_demand');
        $data['recommended_floor'] = $this->input->post('recommended_floor');
        if ($this->input->post('customer_source') != "")
            $data['customer_source'] = "/" . join("/", $this->input->post('customer_source')) . "/";
        $data['source_magazine'] = $this->input->post('source_magazine');
        $data['source_other'] = $this->input->post('source_other');
        if ($this->input->post('negotiate_mode1') != "")
            $data['negotiate_mode1'] = "/" . join("/", $this->input->post('negotiate_mode1')) . "/";
        if ($this->input->post('negotiate_mode2') != "")
            $data['negotiate_mode2'] = "/" . join("/", $this->input->post('negotiate_mode2'));
        if ($this->input->post('key_considerations') != "")
            $data['key_considerations'] = "/" . join("/", $this->input->post('key_considerations')) . "/";
        $data['key_considerations_other'] = $this->input->post('key_considerations_other');
        if ($this->input->post('background_and_characteristics') != "")
            $data['background_and_characteristics'] = join("/", $this->input->post('background_and_characteristics')) . "/";
        $data['dazhi'] = $this->input->post('dazhi');
        $data['rooms'] = $this->input->post('rooms');
        $data['suite'] = $this->input->post('suite');
        $data['adult'] = $this->input->post('adult');
        $data['child'] = $this->input->post('child');
        $data['background_and_characteristics_other'] = $this->input->post('background_and_characteristics_other');
        $data['opinion_floor1'] = $this->input->post('opinion_floor1');
        $data['opinion_money1'] = $this->input->post('opinion_money1');
        $data['opinion_to_money1'] = $this->input->post('opinion_to_money1');
        $data['opinion_floor2'] = $this->input->post('opinion_floor2');
        $data['opinion_money2'] = $this->input->post('opinion_money2');
        $data['opinion_to_money2'] = $this->input->post('opinion_to_money2');
        if ($this->input->post('total_budget') != "")
            $data['total_budget'] = "/" . join("/", $this->input->post('total_budget')) . "/";
        $data['other_total_budget'] = $this->input->post('other_total_budget');
        $data['remark'] = $this->input->post('remark');
        if ($this->input->post('provide_information') != "")
            $data['provide_information'] = "/" . join("/", $this->input->post('provide_information')) . "/";
        $data['provide_information_other'] = $this->input->post('provide_information_other');
        $data['tracking_project'] = $this->input->post('tracking_project');
        $data['competent_suggestions'] = $this->input->post('competent_suggestions');
        $data['head'] = $this->input->post('head');
        $data['completed_by'] = $this->input->post('completed_by');
        $data['front_man'] = $this->login_lib->users_name();
        if ($this->input->post('required_level') != "")
            $data['required_level'] = "/" . join("/", $this->input->post('required_level')) . "/";
        $data['number'] = $this->input->post('number');
        $data['negotiate_year1'] = $this->input->post('negotiate_year1');
        $data['negotiate_year2'] = $this->input->post('negotiate_year2');
        $data['negotiate_month1'] = $this->input->post('negotiate_month1');
        $data['negotiate_month2'] = $this->input->post('negotiate_month2');
        $data['negotiate_time1'] = $this->input->post('negotiate_time1');
        $data['negotiate_time2'] = $this->input->post('negotiate_time2');
        $data['negotiate_time1'] = $this->input->post('negotiate_time1');
        $data['negotiate_time2'] = $this->input->post('negotiate_time2');
        $data['negotiate2'] = $this->input->post('negotiate2');
        $data['b1'] = $this->input->post('b1');
        $data['b2'] = $this->input->post('b2');
        $data['type_consignment'] = $this->input->post('type_consignment');
        $return_result = $this->demand_model->add_demand_consignment($data);
        //echo $this->db->last_query();die();
        if ($return_result > 0) {
            redirect("/demand/data_query_results");
        } else {
            echo "插入失敗";
        }
    }

    public function info($id = "") {
        $html = $this->init("代銷信息", "jquery,jqueryui,consignment", "");
        $this->load->model('demand_model');
        $html["consignment"] = $this->demand_model->get_demand_info($id);
        $this->load->model('consignment_management_model');
        $html['consignment_management'] = $this->consignment_management_model->get_info($html["consignment"]->type_consignment);
        $this->load->view('consignment_info', $html);
    }

    public function edit($id = "") {
        $html = $this->init("代銷修改", "jquery,jqueryui,consignment", "");
        $this->load->model('demand_model');
        $this->load->model('insert_model');

        $html["discuss"] = $this->insert_model->query_table("demand_id", $id, "h_consignment_discuss");
        $html["consignment"] = $this->demand_model->get_demand_info($id);
        $this->load->model('consignment_management_model');
        $html['consignment_management'] = $this->consignment_management_model->get_info($html["consignment"]->type_consignment);
        $this->load->view('consignment_edit', $html);
    }

    public function fixx() {

        $data = $this->input->post('data');

        //print_r($data);

        $p_data = array();

        $call_flag  = true;
        $there_flag = true;
        $visit_flag = true;

        $p_data["call"] = "";
        $p_data["there"] = "";
        $p_data["visit"] = "";
        foreach ($data as $name => $value) {

            if (!empty($value["value"]) AND $value["value"] > 0) {
                $p_data[$value["name"]] = $value["value"];
            }
            
            if ($value["name"]=="ch[]" AND $value["value"] == "call" AND $call_flag) {
                $p_data["call"] = 1;
                $call_flag=false;
            }
            
            if ($value["name"]=="ch[]" AND $value["value"] == "there" AND $there_flag ) {
                $p_data["there"] = 1;
                $there_flag=false;
            }
            
            if ($value["name"]=="ch[]" AND $value["value"] == "visit" AND $visit_flag) {
                $p_data["visit"] = 1;
                $visit_flag=false;
            }
        }

        

        $this->load->model('consignment_model');
        $this->consignment_model->fix($p_data);
    }

    public function edit_submit() {
        $this->load->model('demand_model');
        $id = $this->input->post('demand_id');
        $data['name'] = $this->input->post('name');
        $data['ms'] = $this->input->post('ms');
        $data['mr'] = $this->input->post('mr');
        $data['mobile'] = $this->input->post('mobile');
        $data['home_phone'] = $this->input->post('home_phone');
        $data['company_phone'] = $this->input->post('company_phone');
        $data['home_address'] = $this->input->post('home_address');
        $data['building_name'] = $this->input->post('building_name');
        $data['company_address'] = $this->input->post('company_address');
        $data['company_name'] = $this->input->post('company_name');
        $data['contact'] = $this->input->post('contact');
        $data['contact_phone'] = $this->input->post('contact_phone');
        if ($this->input->post('demand_area') != "")
            $data['demand_area'] = "/" . join("/", $this->input->post('demand_area')) . "/";
        $data['parking_number'] = $this->input->post('parking_number');
        $data['acceptable_mechanical'] = $this->input->post('acceptable_mechanical');
        $data['indoor_area'] = $this->input->post('indoor_area');
        $data['other_demand'] = $this->input->post('other_demand');
        $data['floors'] = $this->input->post('floors');
        $data['windows1'] = $this->input->post('windows1');
        $data['building_facing1'] = $this->input->post('building_facing1');
        $data['facing_the_household1'] = $this->input->post('facing_the_household1');
        $data['seat1'] = $this->input->post('seat1');
        $data['towards1'] = $this->input->post('towards1');
        $data['windows2'] = $this->input->post('windows2');
        $data['building_facing2'] = $this->input->post('building_facing2');
        $data['facing_the_household2'] = $this->input->post('facing_the_household2');
        $data['seat2'] = $this->input->post('seat2');
        $data['towards2'] = $this->input->post('towards2');
        $data['households_demand'] = $this->input->post('households_demand');
        $data['recommended_floor'] = $this->input->post('recommended_floor');
        if ($this->input->post('customer_source') != "")
            $data['customer_source'] = "/" . join("/", $this->input->post('customer_source')) . "/";
        $data['source_magazine'] = $this->input->post('source_magazine');
        $data['source_other'] = $this->input->post('source_other');
        if ($this->input->post('negotiate_mode1') != "")
            $data['negotiate_mode1'] = "/" . join("/", $this->input->post('negotiate_mode1')) . "/";
        if ($this->input->post('negotiate_mode2') != "")
            $data['negotiate_mode2'] = "/" . join("/", $this->input->post('negotiate_mode2')) . "/";
        if ($this->input->post('key_considerations') != "")
            $data['key_considerations'] = "/" . join("/", $this->input->post('key_considerations')) . "/";
        $data['key_considerations_other'] = $this->input->post('key_considerations_other');
        if ($this->input->post('background_and_characteristics') != "")
            $data['background_and_characteristics'] = "/" . join("/", $this->input->post('background_and_characteristics')) . "/";
        $data['dazhi'] = $this->input->post('dazhi');
        $data['rooms'] = $this->input->post('rooms');
        $data['suite'] = $this->input->post('suite');
        $data['adult'] = $this->input->post('adult');
        $data['child'] = $this->input->post('child');
        $data['background_and_characteristics_other'] = $this->input->post('background_and_characteristics_other');
        $data['opinion_floor1'] = $this->input->post('opinion_floor1');
        $data['opinion_money1'] = $this->input->post('opinion_money1');
        $data['opinion_to_money1'] = $this->input->post('opinion_to_money1');
        $data['opinion_floor2'] = $this->input->post('opinion_floor2');
        $data['opinion_money2'] = $this->input->post('opinion_money2');
        $data['opinion_to_money2'] = $this->input->post('opinion_to_money2');
        if ($this->input->post('total_budget') != "")
            $data['total_budget'] = "/" . join("/", $this->input->post('total_budget')) . "/";
        $data['other_total_budget'] = $this->input->post('other_total_budget');
        $data['remark'] = $this->input->post('remark');
        if ($this->input->post('provide_information') != "")
            $data['provide_information'] = "/" . join("/", $this->input->post('provide_information')) . "/";
        $data['provide_information_other'] = $this->input->post('provide_information_other');
        $data['tracking_project'] = $this->input->post('tracking_project');
        $data['competent_suggestions'] = $this->input->post('competent_suggestions');
        $data['head'] = $this->input->post('head');
        $data['completed_by'] = $this->input->post('completed_by');
        if ($this->input->post('required_level') != "")
            $data['required_level'] = "/" . join("/", $this->input->post('required_level')) . "/";
        $data['number'] = $this->input->post('number');
        $data['negotiate_year1'] = $this->input->post('negotiate_year1');
        $data['negotiate_year2'] = $this->input->post('negotiate_year2');
        $data['negotiate_month1'] = $this->input->post('negotiate_month1');
        $data['negotiate_month2'] = $this->input->post('negotiate_month2');
        $data['negotiate_time1'] = $this->input->post('negotiate_time1');
        $data['negotiate_time2'] = $this->input->post('negotiate_time2');
        $data['negotiate_time1'] = $this->input->post('negotiate_time1');
        $data['negotiate_time2'] = $this->input->post('negotiate_time2');
        $data['negotiate2'] = $this->input->post('negotiate2');
        $data['b1'] = $this->input->post('b1');
        $data['b2'] = $this->input->post('b2');
        $return_result = $this->demand_model->update_demand($data, $id);
        if ($return_result) {
            redirect("/demand/data_query_results");
        } else {
            echo "編輯失敗";
        }
    }

    public function discuss_submit() {



        $data['demand_id'] = $this->input->post('demand_id');
        $data['negotiate_year2'] = $this->input->post('negotiate_year2');
        $data['negotiate_month2'] = $this->input->post('negotiate_month2');
        $data['negotiate_day2'] = $this->input->post('negotiate_day2');
        $data['negotiate_time2'] = $this->input->post('negotiate_time2');
        $data['negotiate2'] = $this->input->post('negotiate2');

        $ch = $this->input->post('ch');        

        $data["call"] = "";
        $data["there"] = "";
        $data["visit"] = "";
            
        foreach($ch as $fg=>$value){
            
            if ($value == "call") {

                $data["call"] = 1;
                $call_flag=false;
            }
            
            if ($value == "there") {
                $data["there"] = 1;
                $there_flag=false;
            }
            
            if ($value == "visit" ) {
                $data["visit"] = 1;
                $visit_flag=false;
            }
        }    

        
            
         
        
        $t=time();
            
        $data["create_time"] = date("Y-m-d H:i:s",$t);    

        


        $this->load->model('insert_model');
        $this->insert_model->insert_to_sql($data, "h_consignment_discuss");
        //if($return_result){
        	redirect("/demand/data_query_results");
        //}else{
        //	echo "編輯失敗";
        //}
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */